package com.woniuxy.hotel.room.service.imp;

import java.math.BigDecimal;
import java.util.List;

import com.woniuxy.hotel.room.mapper.RoomTypeMapper;
import com.woniuxy.hotel.room.pojo.*;
import com.woniuxy.hotel.room.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.woniuxy.hotel.room.mapper.RoomMapper;
import com.woniuxy.hotel.room.pojo.RoomExample.Criteria;
import com.woniuxy.hotel.room.service.RoomService;

import javax.annotation.Resource;

@Service
public class RoomServiceImp implements RoomService{
	@Resource
	private  RoomMapper roomMapper;
	@Resource
	private RoomTypeMapper roomTypeMapper;
	
	/*查找所有房间
	 * 
	 * 
	 * */
	@Override
    public List<Room> findAllRoom() {
		return  roomMapper.findAllRoom();
	}

	/*动态SQl查找房间
	 * 所有字段都支持
	 * 
	 * */
	@Override
	public List<Room> findRoom1(Room room,BigDecimal low,BigDecimal high) {
		RoomExample example=new RoomExample();
		Criteria criteria=example.createCriteria();
		criteria.andRoompriceBetween(low,high);
		criteria.andRoomidEqualTo(room.getRoomid());
		criteria.andHotelidEqualTo(room.getHotelid());
		criteria.andRoomnumEqualTo(room.getRoomnum());
		criteria.andDiscountEqualTo(room.getDiscount());
		criteria.andRoomstatusEqualTo(room.getRoomstatus());
		return roomMapper.selectByExampleWithBLOBs(example);
	}
	
	/*
	 * 
	 * 添加房间*/
	@Override
	public int insertRoom(Room room) {
		int a=0;
		List<Room> lroom= roomMapper.findAllRoom();
		for (Room room2 : lroom) {
			if(room2.getRoomid()==room.getRoomid()){
				return a;
			}
		}
		RoomExample example=new RoomExample();
		Criteria criteria=example.createCriteria();
		criteria.andRoomidIsNotNull();
		criteria.andHotelidIsNotNull();
		criteria.andRoomnumIsNotNull();
		criteria.andDiscountIsNotNull();
		criteria.andRoomstatusIsNotNull();
		return roomMapper.insertSelective(room);
	}
	/*
	 * 
	 * 根据房间ID删除房间*/
	@Override
	public int delRoom(Room room) {
		int a=0;
		List<Room> lroom= roomMapper.findAllRoom();
		for (Room room2 : lroom) {
			if(room2.getRoomid()==room.getRoomid()){
				return  roomMapper.deleteByPrimaryKey(room.getRoomid());
			}
		}
		return a;
	}
	/*
	 * 
	 * 动态Sql删除房间*/
	@Override
	public int delRoomSql(Room room) {
		RoomExample example=new RoomExample();
		Criteria criteria=example.createCriteria();
		criteria.andRoomidEqualTo(room.getRoomid());
		criteria.andHotelidEqualTo(room.getHotelid());
		criteria.andRoomnumEqualTo(room.getRoomnum());
		criteria.andDiscountEqualTo(room.getDiscount());
		criteria.andRoomstatusEqualTo(room.getRoomstatus());
		return roomMapper.deleteByExample(example);
	}

	/*动态修改输入的都修改Sql
	 * 所有房间都修改
	 * 修改房间*/
	@Override
	public int updateRoomSql(Room room) {
		RoomExample example=new RoomExample();
		Criteria criteria=example.createCriteria();
		criteria.andRoomidEqualTo(room.getRoomid());
		criteria.andHotelidEqualTo(room.getHotelid());
		criteria.andRoomnumEqualTo(room.getRoomnum());
		criteria.andDiscountEqualTo(room.getDiscount());
		criteria.andRoomstatusEqualTo(room.getRoomstatus());
		return roomMapper.updateByExampleSelective(room, example);
	}
	/**
	 * 根据主键
	 * 对相应房间进行Sql修改*/
	@Override
	public int updateRoomSql1(Room room) {
		return roomMapper.updateByPrimaryKeySelective(room);
	}
	
	@Override
	public int countNum (int flag) {
		RoomExample roomExample=new RoomExample();
		Criteria criteria = roomExample.createCriteria();
		if(flag==0){
			criteria.andRoomFlagEqualTo(0);
		}else if(flag==1){
			criteria.andRoomFlagNotEqualTo(0);
		}
		return roomMapper.countByExample(roomExample);
	}
	
	@Override
	public List<Room> selectAllRoomsInfo (Integer page,Integer limit) {
		PageInfo pageInfo =new PageInfo(page,limit);
		List<Room> list=roomMapper.selectAllRoomsInfo(pageInfo);
		for (Room room : list) {
			room.setHn(room.getHotel().getHotelname());
			room.setRt(room.getRoomType().getRoomtype());
			room.setRt(room.getRoomType().getRoomtype());
		}
		return list;
	}
	
	@Override
	public List<Room> selectDeletedRoomsInfo (Integer page, Integer limit) {
		PageInfo pageInfo =new PageInfo(page,limit);
		List<Room> list=roomMapper.selectDeletedRoomsInfo(pageInfo);
		for (Room room : list) {
			room.setHn(room.getHotel().getHotelname());
			room.setRt(room.getRoomType().getRoomtype());
		}
		return list;
	}
	
	@Override
	public int recoverRoom (Integer roomId) {
		return roomMapper.recoverByRoomId(roomId);
	}
	
	/*
	 *根据酒店Id查找房间
	 * */
	@Override
	public List<Room> findAllRoomThisHotel(Room room) {
		RoomExample example=new RoomExample();
		Criteria criteria=example.createCriteria();
		criteria.andRoomstatusEqualTo(0);
		criteria.andHotelidEqualTo(room.getHotelid());
		//实验
		List<Room> rooms= roomMapper.selectByExampleWithBLOBs(example);
		for (Room room2 : rooms) {
			if(room2.getRoomtypeid()==1){
				room2.setRt("单人房");
				RoomType rt1=roomTypeMapper.selectByPrimaryKey(1);
				room2.setRoomprice(rt1.getRoomprice());
			}else
			if(room2.getRoomtypeid()==2){
				room2.setRt("双人房");
				RoomType rt1=roomTypeMapper.selectByPrimaryKey(2);
				room2.setRoomprice(rt1.getRoomprice());
			}else
			if(room2.getRoomtypeid()==3){
				room2.setRt("大床房 ");
				RoomType rt1=roomTypeMapper.selectByPrimaryKey(3);
				room2.setRoomprice(rt1.getRoomprice());
			}else
			if(room2.getRoomtypeid()==4){
				room2.setRt("亲子房 ");
				RoomType rt1=roomTypeMapper.selectByPrimaryKey(4);
				room2.setRoomprice(rt1.getRoomprice());
			}
		}
		
		return rooms;
		
		
	}
	
	@Override
	public Room findRoomById(int roomI) {
		return roomMapper.selectByPrimaryKey(roomI);
	}
	
	
	@Override
	public int updateByRoomTypeId (Room room) {
		return roomMapper.updateByRoomTypeId(room);
	}
	
	
	
}
